/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit_vendor_management;

import java.util.Date;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author QuangKhoi
 */
public class Vendor {
    public String MaDaiLy = "";
    public VendorType LoaiDL = new VendorType();
    public String TenDaiLy = "";
    public String DienThoai = "";
    public String DiaChi = "";
    public District quan = new District();
    public String MaMatHang = "";
    public Date NgayTiepNhan = new Date();
    public String Email = "";
    public Integer TienNo = 0;
    
    public Vendor(String MaDaiLy)
    {
        QueryResult result = Database.instance.Query("SELECT MaDaiLy, TenDaiLy, "
                + "DienThoai, DiaChi, MaQuan, NgayTiepNhan, Email, TienNo, "
                + "MaLoaiDaiLy FROM "
                + "DaiLy WHERE MaDaiLy='" + MaDaiLy + "'");
        
        if (result.Count == 1)
        {
            this.MaDaiLy = MaDaiLy;
            LoaiDL = new VendorType(result.Table.getString(0, 8));
            TenDaiLy = result.Table.getString(0, 1);
            DienThoai = result.Table.getString(0, 2);
            DiaChi = result.Table.getString(0, 3);
            quan = new District(result.Table.getString(0, 4));
            NgayTiepNhan = result.Table.getDate(0, 5);
            Email = result.Table.getString(0, 6);
            TienNo = result.Table.getInteger(0, 7);
        }
    }
    
    public Vendor() {
        this.MaDaiLy = Database.getID("DAILY", "MaDaiLy");
    }
    
    public Vendor(String MaLoaiDaiLy, String TenDaiLy, String DienThoai, 
            String DiaChi, String MaQuan, Date NgayTiepNhan, String Email, 
            int TienNo)
    {
        this.MaDaiLy = Database.getID("DAILY", "MaDaiLy");
        this.LoaiDL = new VendorType(MaLoaiDaiLy);
        this.TenDaiLy = TenDaiLy;
        this.DienThoai = DienThoai;
        this.DiaChi = DiaChi;
        this.quan = new District(MaQuan);
        this.NgayTiepNhan = NgayTiepNhan;
        this.Email = Email;
        this.TienNo = TienNo;
    }
    
    public Boolean Save()
    {
        QueryResult result = 
                Database.instance.Query("SELECT * FROM DaiLy WHERE MaDaiLy='"
                + MaDaiLy + "'");
        java.sql.Date sqlNgayTiepNhan = new java.sql.Date(NgayTiepNhan.getTime());
        if (result.Count == 1)
        {
            //Update
            Database.instance
                    .Query("UPDATE DaiLy SET TenDaiLy = '" + TenDaiLy + "', " 
                        + " MaLoaiDaiLy = '" + LoaiDL.MaLoaiDaiLy + "', "
                        + " DienThoai = '" + DienThoai + "', "
                        + " DiaChi = '" + DiaChi + "', "
                        + " MaQuan = '" + quan.MaQuan + "', "
                        + " NgayTiepNhan = '" + sqlNgayTiepNhan + "', "
                        + " Email = '" + Email + "', "
                        + " TienNo = '" + TienNo + "' "
                        + " WHERE MaDaiLy = '" + MaDaiLy + "'");
        }
        else
        {
            //Add New
            Database.instance
                    .Query("INSERT INTO DaiLy (MaDaiLy, MaLoaiDaiLy, TenDaiLy, "
                    + "DienThoai, DiaChi, MaQuan, NgayTiepNhan, Email, TienNo) "
                    + "VALUE ('" + MaDaiLy + "', '" + TenDaiLy + "', "
                    + DienThoai + "', " + DiaChi + "', " + quan.MaQuan + "', "
                    + sqlNgayTiepNhan + "', " + Email + "', " + TienNo + "')");
        }
        return true;
    }
    
    public Boolean Delete()
    {
        Database.instance.Query("DELETE FROM DaiLy WHERE MaDaiLy='"
                + MaDaiLy + "'");
        return true;
    }
    
    public static DefaultTableModel getModel() {
        QueryResult result = 
                Database.instance.Query("SELECT MaDaiLy as 'Mã Đại Lý', "
                + "TenDaiLy as 'Tên Đại Lý', "
                + "TenLoaiDaiLy as 'Loại', "
                + "TenQuan as 'Quận', "
                + "TienNo as 'Tiền Nợ' "
                + "FROM DAILY "
                + "LEFT JOIN LOAIDAILY "
                + "ON DAILY.MaLoaiDaiLy = DAILY.MaLoaiDaiLy "
                + "LEFT JOIN QUAN "
                + "ON DAILY.MaQuan = QUAN.MaQuan");
        return result.Table;
    }
}
